﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General

Namespace DoAnQuanLyBanHang.DTO
    Public Class KhachHangData
        Private m_KhachHangData As New DataService()

        Public Function LayKhachHang() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Khach_Hang")
            m_KhachHangData.Load(cmd)
            Return m_KhachHangData
        End Function

        Public Function LayKHTheoMa(id As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Khach_Hang WHERE MaKH LIKE '%' + @id + '%'")
            cmd.Parameters.Add("id", SqlDbType.NVarChar).Value = id

            m_KhachHangData.Load(cmd)
            Return m_KhachHangData
        End Function

        Public Function LayKHTheoTen(ten As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Khach_Hang WHERE HoTen LIKE '%' + @ten + '%'")
            cmd.Parameters.Add("ten", SqlDbType.NVarChar).Value = ten

            m_KhachHangData.Load(cmd)
            Return m_KhachHangData
        End Function

        Public Function LayMaKHTheoTen(ten As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT MaKH FROM Khach_Hang WHERE HoTen LIKE '%' + @ten + '%'")
            cmd.Parameters.Add("ten", SqlDbType.NVarChar).Value = ten

            m_KhachHangData.Load(cmd)
            Return m_KhachHangData
        End Function

        Public Function ThemDongMoi() As DataRow
            Return m_KhachHangData.NewRow()
        End Function

        Public Sub ThemKhachHang(m_Row As DataRow)
            m_KhachHangData.Rows.Add(m_Row)
        End Sub

        Public Function LuuKhachHang() As Boolean
            Return m_KhachHangData.ExecuteNoneQuery() > 0
        End Function
        Public Sub LuuKhachHang(makh As [String], hoten As [String], diachi As [String], dienthoai As [String])
            Dim cmd As New SqlCommand("INSERT INTO Khach_Hang VALUES(@makh, @hoten, @diachi, @dienthoai)")
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            cmd.Parameters.Add("hoten", SqlDbType.NVarChar).Value = hoten
            cmd.Parameters.Add("diachi", SqlDbType.NVarChar).Value = diachi
            cmd.Parameters.Add("dienthoai", SqlDbType.VarChar).Value = dienthoai

            m_KhachHangData.Load(cmd)
        End Sub

    End Class
End Namespace
